package org.jeecg.modules.workflow.controller.change;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.boc.ewr.business.entity.ProtocolChangeApply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.workflow.vo.change.protocol.BpmChgProtocolCreateReqVo;
import org.jeecg.common.workflow.vo.change.protocol.BpmChgProtocolPageReqVo;
import org.jeecg.common.workflow.vo.change.protocol.BpmChgProtocolRespVo;
import org.jeecg.common.workflow.vo.change.protocol.BpmChgProtocolUpdateReqVo;
import org.jeecg.common.workflow.vo.oa.BpmOALeaveUpdateReqVO;
import org.jeecg.modules.workflow.controller.BaseController;
import org.jeecg.modules.workflow.convert.change.BpmChgProtocolConvert;
import org.jeecg.modules.workflow.service.change.BpmChgProtocolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author : Wzz
 * @create : 2023-04- 27- 10:23
 * @desc :
 */
@Api(tags = "管理后台 - 协议变更申请")
@RestController
@RequestMapping("/bpm/chapro/leave")
@Validated
public class BpmChgProtocolController extends BaseController {

    @Resource
    private BpmChgProtocolService service;

    @Autowired
    private BpmChgProtocolConvert convert;

    @PostMapping("/create")
    //@RequiresPermissions("bpm:chapro:create")
    @ApiOperation("创建请求申请")
    public Result<Long> createLeave(@Valid @RequestBody BpmChgProtocolCreateReqVo createReqVO) {
        return Result.OK(service.createChgProtocol(getUserId(), createReqVO));
    }

    @GetMapping("/get")
    //@RequiresPermissions("bpm:chapro:query")
    @ApiOperation("获得请假申请")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public Result<BpmChgProtocolRespVo> getLeave(@RequestParam("id") String id) {
        ProtocolChangeApply chgProtocol = service.getChgProtocol(id);
        return Result.OK(convert.convert(chgProtocol));
    }

    @GetMapping("/page")
    //@RequiresPermissions("bpm:chapro:query")
    @ApiOperation("获得请假申请分页")
    public Result<IPage<BpmChgProtocolRespVo>> getLeavePage(@Valid BpmChgProtocolPageReqVo pageVO) {
        IPage<ProtocolChangeApply> chgSnorgPage = service.getChgProtocolPage(getUserId(), pageVO);
        IPage<BpmChgProtocolRespVo> bpmChgProtocolRespVoIPage = convert.convertPage(chgSnorgPage);
        return Result.OK(bpmChgProtocolRespVoIPage);
    }


    @ApiOperation(value = "协议变更申请-通过协议号获取信息", notes = "协议变更申请-通过协议号获取信息")
    //@RequiresPermissions("bpm:chapro:getInfo")
    @GetMapping(value = "/getInfo/{protocolNo}")
    public Result<BpmChgProtocolRespVo> getInfoByProtocolNo(@PathVariable String protocolNo) {
        BpmChgProtocolRespVo bpmChgProtocolRespVo = service.getInfoByProtocolNo(protocolNo);
        return Result.ok(bpmChgProtocolRespVo);
    }

    @PutMapping("/update")
    //@PreAuthorize("@ss.hasPermi('bpm:chapro:update')")
    @ApiOperation("更新协议变更申请")
    public Result<Boolean> updateLeave(@Valid @RequestBody BpmChgProtocolUpdateReqVo updateReqVO) {
        service.updateChgProtocol(getUserId(), updateReqVO);
        return Result.OK(true);
    }

    private String getUserId() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser.getId();
    }

}
